<template>
	<view>
		<view class="bg padding margin radius">
			<view class="flex align-center justify-between">
				<view style="font-size: 30;">开始时间</view>
				<view class="flex align-center" @click="startShow = true">
					<view style="color: #999999;" v-if="!startTime">请选择开始时间</view>
					<view style="color: #999999;">{{startTime}}</view>
					<image src="../static/icon-right.png" class="margin-left-sm" style="width: 14upx;height: 24upx;">
					</image>
				</view>
			</view>
			<view class="margin-tb" style="width: 100%;height: 1rpx;background: #F2F2F2;"></view>
			<view class="flex align-center justify-between">
				<view style="font-size: 30;">结束时间</view>
				<view class="flex align-center" @click="endShow = true">
					<view style="color: #999999;" v-if="!endTime">请选择结束时间</view>
					<view style="color: #999999;">{{endTime}}</view>
					<image src="../static/icon-right.png" class="margin-left-sm" style="width: 14upx;height: 24upx;">
					</image>
				</view>
			</view>
		</view>

		<view class="bg padding-lr padding-tb-sm margin radius">
			<view class="flex align-center justify-between">
				<view style="font-size: 30;">预约人数</view>
				<view class="text-align: right;">
					<u-input v-model="number" type="number" placeholder="请填写预约人数" style="text-align: right;"
						inputAlign="text-alight:right" />
				</view>
			</view>
		</view>
		<view class="butt margin-lr" @click="addData">添加时间</view>
		<!-- 时间筛选 -->
		<u-picker v-model="startShow" mode="time" :params="params" @confirm="confirm"></u-picker>

		<u-picker v-model="endShow" mode="time" :params="paramss" @confirm="getEndtime"></u-picker>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				number: '',
				startTime: '',
				endTime: '',
				startShow: false,
				endShow: false,
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				paramss: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				serviceTimeId: ''

			}
		},
		onLoad(option) {
			if (option.serviceTimeId) {
				this.serviceTimeId = option.serviceTimeId
				this.dataDetail()
			}
		},
		onShow() {

		},
		methods: {
			confirm(e) {
				// console.log(e)
				this.startTime = e.hour + ':' + e.minute
			},
			getEndtime(e) {
				this.endTime = e.hour + ':' + e.minute

			},
			addData() {
				if (!this.startTime) {
					uni.showToast({
						title: '请选择开始时间',
						icon: 'none'
					})
					return
				}
				if (!this.endTime) {
					uni.showToast({
						title: '请选择结束时间',
						icon: 'none'
					})
					return
				}
				if (!this.number) {
					uni.showToast({
						title: '请填写预约人数',
						icon: 'none'
					})
					return
				}
				let serviceTime = this.startTime + '~' + this.endTime
				if(this.serviceTimeId){
					console.log('修改')
					let data = {
						serviceTime: serviceTime,
						num: this.number,
						serviceTimeId: this.serviceTimeId,
					}
					
					this.$Request.postJson('/app/serviceTime/updateServiceTime', data).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
							uni.navigateBack()
						}
					})
				}else{
					let data = {
						serviceTime: serviceTime,
						num: this.number,
					}
					this.$Request.postJson('/app/serviceTime/insertServiceTime', data).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '添加成功',
								icon: 'none'
							})
							uni.navigateBack()
						}
					})
				}
				
			},
			dataDetail() {
				let userId = uni.getStorageSync('userId');
				let data = {
					serviceTimeId: this.serviceTimeId,
					userId: userId
				}
				this.$Request.getT('/app/serviceTime/selectServiceTime', data).then(res => {
					if (res.code == 0) {
						if(res.data){
							let serviceTime = res.data[0].serviceTime.split('~')
							this.startTime  = serviceTime[0]
							this.endTime = serviceTime[1]
							this.number  = res.data[0].num
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background: #F5F5F5;
	}

	.bg {
		background: #fff;
	}

	.butt {
		background: #557EFD;
		color: #fff;
		text-align: center;
		margin-top: 90upx;
		padding: 25upx 30upx;
		border-radius: 24upx;

	}
</style>
